package cn.zhys513.opencv;

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;

public class cv2 extends PointTransform{
	
    static {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }
    
    public static void print(Object msg) {
    	System.out.println(msg);
    }
 
	/**
	 * 从边缘图中寻找轮廓
	 * @param image
	 * @param hierarchy
	 * @param mode
	 * @param method
	 * @return
	 */
	public static List<MatOfPoint> findContours(Mat image, Mat hierarchy, int mode, int method){
		List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
		findContours(image, contours, hierarchy, mode, method);
		return contours;
	}
 
	/**
	 * 从边缘图中寻找轮廓
	 * @param image
	 * @param mode
	 * @param method
	 * @return
	 */
	public static List<MatOfPoint> findContours(Mat image, int mode, int method){
		List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
		Mat hierarchy = new Mat();
		findContours(image, contours, hierarchy, mode, method);
		return contours;
	}
	
	
	/**
	 * @param srcFilePath
	 * @return
	 */
	public static Mat imread(String srcFilePath){
		return Imgcodecs.imread(srcFilePath);
	}
	
 
	/**
	 * 高斯模糊
	 * @param image
	 * @param width
	 * @param height
	 * @param sigmaX
	 * @return
	 */
	public static Mat GaussianBlur(Mat image,double width, double height,double sigmaX) {
		Mat dst = new Mat();
		GaussianBlur(image, dst, new Size(width, height), sigmaX);
		return dst;
	}
	
	 
	/**
	 * 高斯滤波，降噪
	 * @param image
	 * @param width
	 * @param height
	 * @param sigmaX
	 * @param sigmaY
	 * @return
	 */
	public static Mat GaussianBlur(Mat image,double width, double height,double sigmaX,double sigmaY) {
		Mat dst = new Mat();
		GaussianBlur(image, dst, new Size(width, height), sigmaX,sigmaY);
		return dst;
	}
	
 
	/**
	 * 边缘检测
	 * 算子
	 * @param image
	 * @param threshold1
	 * @param threshold2
	 * @return
	 */
	public static Mat Canny(Mat image,double threshold1, double threshold2) {
		Mat dst = new Mat();
		Canny(image, dst, threshold1, threshold2);
		return dst;
	}
	
	/**
	 * 边缘检测
	 * 算子
	 * @param image
	 * @param threshold1
	 * @param threshold2
	 * @param apertureSize
	 * @param L2gradient
	 * @return
	 */
	public static Mat Canny(Mat image,double threshold1, double threshold2,int apertureSize,boolean L2gradient) {
		Mat dst = new Mat();
		Canny(image, dst, threshold1, threshold2,apertureSize, L2gradient);
		return dst;
	}
	
	 
	/**
	 * 灰度
	 * @param image
	 * @param code
	 * @return
	 */
	public static Mat cvtColor(Mat image,int code) {
		// 灰度
		Mat gray = new Mat();
		cvtColor(image, gray, code);
		return gray;
	}
	
 
	/**
	 * 膨胀，连接边缘
	 * @param image
	 * @param px
	 * @param py
	 * @param iterations
	 * @param borderType
	 * @param s
	 * @return
	 */
	public static Mat dilate(Mat image,double px, double py,int iterations, int borderType,double s) {
		Mat dst = new Mat();
        dilate(image, dst, new Mat(), new Point(px,py), iterations, borderType, new Scalar(s));
		return dst;
	}
	
	/**
	 * 对原始图像和灰度图都进行四点透视变换
	 * @param src
	 * @param contours
	 * @return
	 */
	public static Mat FourPointTransform(Mat src,List<MatOfPoint> contours) {
		return  fourPointTransform(src,contours,false);
	}
	 
	
	/**
	 * 显示图片
	 * @param image
	 */
	public static void imshow(Mat image) {
		ImageViewer ImageViewer = new ImageViewer(image);
		ImageViewer.imshow();
	}
	
	/**
	 * 显示图片
	 * @param image
	 * @param windowName
	 */
	public static void imshow(Mat image, String windowName) {
		ImageViewer ImageViewer = new ImageViewer(image,windowName);
		ImageViewer.imshow();
	}
}
